package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.BenefitEntity;
import com.dragon.modules.yuegou.entity.member.TemplateGradeEntity;
import com.dragon.modules.yuegou.entity.member.TemplateRightsEntity;
import com.dragon.modules.yuegou.enums.member.BenefitBenefitType;
import com.dragon.modules.yuegou.enums.member.TemplateRightsRightsType;
import com.dragon.modules.yuegou.mapper.member.BenefitMapper;
import com.dragon.modules.yuegou.query.member.BenefitQuery;
import com.dragon.modules.yuegou.query.member.TemplateGradeQuery;
import com.dragon.modules.yuegou.query.member.TemplateRightsQuery;
import com.dragon.modules.yuegou.response.MemberBenefitResponse;
import com.dragon.modules.yuegou.service.member.BenefitService;
import com.dragon.modules.yuegou.service.member.TemplateGradeService;
import com.dragon.modules.yuegou.service.member.TemplateRightsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商圈权益表 服务实现
 *
 * @author pbuilder generated
 * @date 2021-12-15 09:38:01
 * @version 2.0
 */
@Service("memBenefitService")
public class BenefitServiceImpl
		extends ServiceImpl<BenefitMapper, BenefitEntity>
		implements BenefitService {


	@Autowired
	private TemplateRightsService templateRightsService;

	@Autowired
	private TemplateGradeService templateGradeService;

	@Autowired
	private BenefitService benefitService;

	@Override
	public List<String> checkBenefit(String rightsId, TemplateRightsRightsType templateRightsRightsType) {
		List<String> response = new ArrayList<>();
		TemplateRightsQuery query = new TemplateRightsQuery();
		query.setMemRightsId(rightsId);
		query.setRightsType(templateRightsRightsType);
		List<TemplateRightsEntity> list = templateRightsService.list(query.toQueryWrapper());
		if(CollectionUtils.isNotEmpty(list)){
			List<String> gradeIdList =  list.stream().map(item -> item.getTemplateGradeId()).collect(Collectors.toList());
			TemplateGradeQuery gradeQuery = new TemplateGradeQuery();
			gradeQuery.id().in(gradeIdList);
			List<TemplateGradeEntity> templateGrades = templateGradeService.list(gradeQuery.toQueryWrapper());
			if(CollectionUtils.isNotEmpty(templateGrades)){
				response = templateGrades.stream().map(TemplateGradeEntity::getName).collect(Collectors.toList());
			}
		}
		return response;
	}

	@Override
	public MemberBenefitResponse getMemberBenefit(String busdId) {
		BenefitQuery query = new BenefitQuery();
		query.setBusdId(busdId);
		List<BenefitEntity> benefits = benefitService.list(query.toQueryWrapper());
		MemberBenefitResponse response = new MemberBenefitResponse();
		for (BenefitEntity benefit: benefits) {
			if(BenefitBenefitType.DISCOUNT == benefit.getBenefitType()){
				response.setDiscountStatus(true);
			}
			if(BenefitBenefitType.MAGNIFICATION == benefit.getBenefitType()){
				response.setMagnificationPointStatus(true);
			}
		}
		return response;
	}
}